
PLUG.Platform = PLUG_PLATFORM_SHARED

PLUG.ClassName = "sh_playerident"
PLUG.PrintName = "Player Identification Module"
PLUG.Author = "thomasfn"

PLUG.Hard = true

local gid = 0

function PLUG:AddHooks()
	plugs.Hook( PLUG_PLATFORM_SERVER, "PlayerInitialSpawn" )
end

function PLUG:PlayerInitialSpawn( ply )
	ply:AssignID()
	Msg( "Assigned GID '" .. tostring( ply:GetGID() ) .. "' to " .. ply:Nick() .. "!\n" )
end

function PLUG:PlayerExtend( meta )
	if (SERVER) then
		function meta:AssignID()
			gid = gid + 1
			self._GID = gid
			self:SetNWInt( "gid", gid )
		end
	end
	function meta:GetGID()
		if (SERVER) then return self._GID end
		if (CLIENT) then return self:GetNWInt( "gid" ) end
	end
end

function player.GetByGID( id )
	for _, ply in pairs( player.GetAll() ) do
		if (ply:GetGID() == id) then return ply end
	end
end

local function PrintConsole( pl, text )
	pl:PrintMessage( HUD_PRINTCONSOLE, text )
end

if (SERVER) then
	concommand.Add( "getplayergids", function( pl )
		PrintConsole( pl, "GManage - Player GIDs" )
		for _, ply in pairs( player.GetAll() ) do
			PrintConsole( pl, ply:Nick() .. " - " .. ply:SteamID() .. " - " .. ply:GetGID() )
		end
	end )
	concommand.Add( "runplayerfunction", function( pl, com, args )
		if (!pl:IsSuperAdmin()) then return end
		local id = tonumber( args[1] or "0" )
		local ply = player.GetByGID( id )
		if ((!ply) || (!ply:IsValid())) then return end
		_R.Player[ args[2] or "" ]( ply )
	end )
end